/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef HOS_CAMERA_CODEC_NODE_H
#define HOS_CAMERA_CODEC_NODE_H

// #include <vector>
// #include <condition_variable>
// #include <mutex>
#include <ctime>
// #include <jpeglib.h>
// #include "device_manager_adapter.h"
// #include "utils.h"
#include "camera.h"
#include "node_base.h"
// #include  "refbase.h"
#include  "avch264encclient.h"
// #include "avch264enc.h"
// extern "C" {
// #include <MemDmabuf2.h>
// #include "sprd_ion.h"
// #include "jpeg_api.h"
// }

namespace OHOS::Camera {

// struct sprd_jpeg_context {
//     sptr<MemDmabuf2> input_y_pmem_hp;
//     int input_y_pmem_fd;
//     unsigned char* input_y_virtual_addr;

//     sptr<MemDmabuf2> jpg_pmem_hp;
//     size_t jpg_pmemory_size;
//     int input_jpg_pmem_fd;
//     unsigned char* jpg_virtual_addr;
// };

enum VIDEO_RECORD_STATUS 
{
    START,
    ENCODING,
    STOP
};

class CodecNode : public NodeBase {
public:
    CodecNode(const std::string& name, const std::string& type, const std::string &cameraId);
    ~CodecNode() override;
    CameraId ConvertCameraId(const std::string &cameraId);
    RetCode Start(const int32_t streamId) override;
    RetCode Stop(const int32_t streamId) override;
    void DeliverBuffer(std::shared_ptr<IBuffer>& buffer) override;
    virtual RetCode Capture(const int32_t streamId, const int32_t captureId) override;
    RetCode CancelCapture(const int32_t streamId) override;
    RetCode Flush(const int32_t streamId);

    static AvcH264EncInterface* CreateAvcH264EncInterface();
    static void* DlOpenJpegWrapperLib();
    // static void sprd_jpeg_evt_cb(enum jpg_jpeg_evt evt, void* data, void*privdata);
    // static struct sprd_jpeg_context g_sprd_jpeg_cxt_ptr;
    // static int jpeg_length;
private:
    // void encodeJpegToMemory(unsigned char* image, int width, int height,
    //         const char* comment, unsigned long* jpegSize, unsigned char** jpegBuf);
    unsigned char Clip(int value);
    void YUVToRGB(int Y, int U, int V, unsigned char* Red, unsigned char* Green, unsigned char* Blue, unsigned char*Alapha);
    // void YuvBufferToRGBA8888(void *src1, unsigned char *dst, int width, int height);
    // void Yuv420SwToRGBA8888(std::shared_ptr<IBuffer>& buffer);
    // void YUV420ToRGBA8888WithNeon(std::shared_ptr<IBuffer>& buffer);
    // int sprd_jpeg_mem_alloc(uint32_t width, uint32_t height, void* data);
    // int sprd_jpg_mem_release(void);
    RetCode ConfigJpegOrientation(common_metadata_header_t* data);
    RetCode ConfigJpegQuality(common_metadata_header_t* data);
    RetCode Config(const int32_t streamId, const CaptureMeta& meta);
    void Yuv420ToJpegWithUnisoc(std::shared_ptr<IBuffer>& buffer);
    int Yuv420ToH264WithUnisoc(std::shared_ptr<IBuffer>& buffer, uint32_t& frameSize);

    static uint32_t previewWidth_;
    static uint32_t previewHeight_;
    static uint32_t rgbaSize_;
    static AvcH264EncInterface* h264Encoder;
    static void* jpeghandler;
    unsigned char * previewTempBuff_ = nullptr;

    VIDEO_RECORD_STATUS status = START;
    int nodestatus = 0;
    std::vector<std::shared_ptr<IPort>> outPutPorts_;
    std::shared_ptr<IBufferPool>          bufferPool_ = nullptr;    // buffer pool of branch stream
    uint32_t jpegRotation_;
    uint32_t jpegQuality_;
    u_char* bufferRotate_ = NULL;
};
}// namespace OHOS::Camera
#endif
